<?php
/**
 * TeamPlayersMysqli.class.php
 *
 * PHP version 5
 *
 * Copyright (c) 2006-2009, Code Coop SA  http://code.coop
 * Copyright (c) 2010, Leo Valen  http://www.leovalen.no
 * All rights reserved.
 *
 * @author		Leo Valen <leo@code.coop>
 * @author		Fredrik Larsen <f@code.coop>
 */
/**
 * Mysqli extensions
 *
 * @version		1.0
 */
namespace TeamPlayers;
class mysqli extends \mysqli
{
	/**
	 * Query count
	 *
	 * @var number
	 * @access public
	 */
	public $queryCount;
	/**
	 * Whether this instance is currently connected to a database
	 *
	 * @var boolean
	 * @access public
	 */
	public $connected;
	/**
	 * Whether autocommit is on
	 *
	 * @var boolean
	 * @access public
	 */
	public $autocommit;
	/**
	 * Whether mysqli has been initialized
	 *
	 * @var boolean
	 * @access public
	 */
	public $mysqliInitialized;
	/**
	 * Sets initial values
	 *
	 * @return		void
	 * @access		public
	 */
	function __construct ()
	{
		$this->queryCount = null;
		$this->mysqliInitialized = false;
	}
	/**
	 * Calls parent constructor
	 *
	 * @param		string $query
	 * @return		object result
	 * @access		public
	 */
	public function initializeMysqli ()
	{
		if ( ! $this->mysqliInitialized )
		{
			parent::__construct( 
				TeamPlayersMysqlHost,
				TeamPlayersMysqlUser,
				TeamPlayersMysqlPassword,
				TeamPlayersMysqlDatabase
			);
			$this->mysqliInitialized = true;
			$this->queryCount = 0;
		//	$this->query( "SET time_zone = 'GMT'" );
		}
	}
	/**
	 * Transfers a result set from the last query
	 *
	 * @return		object result
	 * @access		public
	 */
	public function store_result ( )
	{
		$result = parent::store_result();

		if ( ! isset( $this->queryCount ) )
		{
			$this->queryCount = 0;
			@$this->set_charset( 'utf8' );
			$this->query( "SET time_zone = 'GMT'" );
		}
		$this->queryCount++;

		return $result;
	}
	/**
	 * Performs a database query
	 *
	 * @param		string $query
	 * @return		object result
	 * @access		public
	 */
	public function query ( $string )
	{
		if ( ! $this->mysqliInitialized )
		{
			$this->initializeMysqli();
		}
		if ( $this->queryCount == 0 )
		{
			@$this->set_charset( 'utf8' );
		}
		if ( $result = @parent::query( $string ) )
		{
		 //   echo str_replace( "\t", "", $string . "<br />\r\n" );
			$this->queryCount++;
			return $result;
		}
		else
		{					
			return false;
		}
	}
	/**
	 * Performs multiple database queries
	 *
	 * @param		string $query
	 * @return		object result
	 * @access		public
	 */
	public function multi_query ( $string )
	{
		if ( ! $this->mysqliInitialized )
		{
			$this->initializeMysqli();
		}
		if ( ! isset( $this->queryCount ) )
		{
			$this->queryCount = 0;
			$this->set_charset( 'utf8' );
			$this->query( "SET time_zone = 'GMT'" );
		}
		if ( $result = @parent::multi_query( $string ) )
		{
			do
			{
        		if ($this->more_results())
				{
					$this->queryCount++;
        		}
   			 } while ( $this->next_result() );

			return $result;
		}
		else
		{					
			return false;
		}
	}
	/**
	 * Initializes mysqli and calls parent
	 *
	 * @param		string $query
	 * @return		object result
	 * @access		public
	 */
	public function real_escape_string ( $string )
	{
		if ( ! $this->mysqliInitialized )
		{
			$this->initializeMysqli();
		}
		return parent::real_escape_string( $string );
	}
	/**
	 * Performs a select query and returns an associative array or false on failure
	 *
	 * @param		string $query
	 * @return		mixed
	 * @access		public
	 */
	public function selectAssoc ( $query )
	{
		$array = array();
		if ( $result = $this->query( $query ) )
		{
			while ( $row = $result->fetch_assoc() )
			{
				$array[] = $row;
			}
			return $array;
		}
		else
		{
			return false;
		}
	}
	/**
	 * Inserts an associative array as a row into the specified table
	 *
	 * @param		string $table			Name of the table or view
	 * @param		array $row				Associative array of data that will be inserted
	 * @param		array $noEscaping		Array of keys for values that should not be escaped (such as mysql statements)
	 * @param		string $insertMethod	Which method to use for inserting (INSERT or REPLACE)
	 * @return		boolean					False on internal failure, or return value from mysqli
	 * @access		public
	 */
	public function insertAssoc ( $table, $row, $noEscaping = array(), $insertMethod = "INSERT" )
	{
		$sql = "{$insertMethod} INTO `{$table}` SET ";
		foreach ( $row as $key => $value )
		{
			if ( ! in_array( $key, $noEscaping ) )
			{
				$value = $this->real_escape_string( $value );
			}
			if ( ! is_int( $value ) && ! in_array( $key, $noEscaping ) )
			{
				$value = "'" . $value . "'";
			}
			$sql .= "`{$key}` = {$value}, ";
		}
		$sql = rtrim( $sql, ", " );
		return $this->query( $sql );
	}
	/**
	 * Sets autocommit flag
	 *
	 * @param		boolean
	 * @return		boolean
	 * @access		public
	 */
	public function autocommit ( $flag )
	{
		$this->autocommit = $flag;
		parent::autocommit( $flag );
	}
	/**
	 * Gets a single variable from database.
	 *
	 * @param		string $query
	 * @return		mixed
	 * @access		public
	 */	
	public function fetchVariable ( $sql )
	{
		$result = @$this->query( $sql );
		if ( $result )
		{
			if ( $row = $result->fetch_row() )
			{
			    $var = $row[0];
			    return $var;
		    }
		}
		return false;
	}
}
?>